//
//  UIImage+Common.m
//  GeniusHelp_OC
//
//  Created by 8b410-2 on 16/10/22.
//  Copyright © 2016年 8b410-2. All rights reserved.
//

#import "UIImage+Common.h"

@implementation UIImage (Common)
//颜色转换 背景图片
+ (UIImage *)imageWithColor:(UIColor *)color {
    return [self imageWithColor:color AndSize:CGSizeMake(1, 1)];
}
+ (UIImage *)imageWithColor:(UIColor *)color AndSize:(CGSize)size {
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
    
}
- (UIImage *)imageWithScale:(CGFloat)scale {
    CGFloat width = self.size.width;
    CGFloat height = width / scale;
    CGFloat y = self.size.height / 2 - height / 2;
    CGRect rect = CGRectMake(0, y, width, height);
    CGImageRef sourceImageRef = self.CGImage;
    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
    return [UIImage imageWithCGImage:newImageRef];
}
- (UIImage *)abb_drawRoundedCornerRectWithRadius:(CGFloat)radius SizeToFit:(CGSize)sizeToFit {
    CGRect rect = CGRectMake(0, 0, sizeToFit.width, sizeToFit.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, false, [UIScreen mainScreen].scale);
    CGContextAddPath(UIGraphicsGetCurrentContext(),
                     [UIBezierPath bezierPathWithRoundedRect:rect
                                           byRoundingCorners:UIRectCornerAllCorners
                                                 cornerRadii:CGSizeMake(radius, radius)]
                     .CGPath);
    CGContextClip(UIGraphicsGetCurrentContext());
    [self drawInRect:rect];
    CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathFillStroke);
    UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return outputImage;
}
@end
